from flask import Blueprint
from flask import request
from model.model import *
images=Blueprint('/images',__name__)
@images.route('/img')
def img_get():
    aa=Img.query.filter().order_by(Img.heat.desc())
    temp=[]
    for i in aa:
        temp.append({
            'id':i.id,
            'title':i.title,
            'img':i.img,
            'url':i.url,
            'heat':i.heat
        })
    return ({'code':200,'data':temp})


@images.route('/img',methods=['POST'])
def img_add():
    params=request.form
    print(params)
    title=params.get('title')
    img=request.files.get('img')
    url=params.get('url')
    img=Img(title=title,img=img,url=url)
    print(img)

    db.session.add(img)
    db.session.commit()

    return ({'code':200,'data':123})

@images.route('/img',methods=['put'])
def img_update():
    params=dict(request.form)
    pid=params.pop('pid')
    print(pid)
    title=params.get('title')
    img=request.files.get('img')
    url=params.get('url')

    Img.query.filter_by(id=pid).update({"title":title,"img":img,"url":url})

    db.session.commit()

    return ({'code':200,'data':123})


@images.route('/img',methods=['delete'])
def img_delete():
    params=dict(request.form)
    pid=params.pop('pid')
    img=Img.query.filter_by(id=pid).first()
    db.session.delete(img)
    db.session.commit()
    temp=Img.query.all()
    aaa=[]
    for i in temp:
        aaa.append({
            'id':i.id,
            'title':i.title,
            'url':i.url,
            'img': i.img,
        })
    return ({'code':200,'data':aaa})


#+++++++++++++++活动页_+++++++++++++++
@images.route('/activity')
def activity_get():
    aa=activity.query.filter_by().order_by(activity.heat.desc())
    temp=[]
    for i in aa:
        temp.append({
            'id':i.id,
            'img':i.img,
            'img_id': i.img_id,
            'heat':i.heat

        })
    return ({'code':200,'data':temp})

@images.route('/activity',methods=['post'])
def activity_add():
    params=request.json
    pid=params.pop('pid')
    AAA=activity(**params)
    db.session.add(AAA)
    db.session.commit()

    aa=activity.query.all()
    temp=[]
    for i in aa:
        temp.append({
            'id':i.id,
            'img':i.img,
            'img_id': i.img_id
        })
    return ({'code':200,'data':temp})


@images.route('/activity',methods=['put'])
def activity_update():
    params=request.json
    pid=params.pop('pid')
    print(pid)
    activity.query.filter_by(id=pid).update({"img":params.get('img'),"img_id":params.get('img_id')})
    db.session.commit()

    aa=activity.query.all()
    temp=[]
    for i in aa:
        temp.append({
            'id':i.id,
            'img':i.img,
            'img_id': i.img_id
        })
    return ({'code':200,'data':temp})


@images.route('/activity',methods=['delete'])
def activity_delete():
    params=request.json
    pid=params.pop('pid')
    act=activity.query.filter_by(id=pid).first()

    db.session.delete(act)
    aa=activity.query.all()
    temp=[]
    for i in aa:
        temp.append({
            'id':i.id,
            'img':i.img,
            'img_id': i.img_id
        })
    return ({'code':200,'data':temp})
